#include "timesettings.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QSpinBox>
#include <QLabel>
#include <QPushButton>
#include <time.h>
#include <QDebug>
#include <QTime>
#include <sys/time.h>
#include <unistd.h>
#include <QDateTime>
#include "analogclock.h"
#include <QGridLayout>
#include <QTimer>
#include "desktopwidget.h"
int sethour    = 0;
int setmin     = 0;
int setsecond  = 0;
int setyear    = 0;
int setmonth   = 0;
int setday     = 0;

TimeSettings2::TimeSettings2(QWidget *parent) : QDialog(parent)
{
    QLabel *pHouLab1 = new QLabel(tr("时"));
    QLabel *pMinLab1 = new QLabel(tr("分"));
    QLabel *pSencondLab1 = new QLabel(tr("秒"));
    QLabel *pYearLab1 = new QLabel(tr("年"));
    QLabel *pMoomlab1 = new QLabel(tr("月"));
    QLabel *pDaylab1 = new QLabel(tr("日"));

    int bt_w, bt_h;

    pHouLab2 = new QLCDNumber;
    pMinLab2 = new QLCDNumber;
    pSencondLab2 = new QLCDNumber;
   // QLabel *pYearLab2 = new QLabel(tr(" "));
    pMonthlab2 = new QLCDNumber;
    pDaylab2 =  new QLCDNumber;
    pYearLab2 = new QLCDNumber;
	
	bool ok;

	setyear 	= QDateTime::currentDateTime().date().toString("yyyy").toInt(&ok, 10);
	setmonth 	= QDateTime::currentDateTime().date().toString("MM").toInt(&ok, 10);
	setday		= QDateTime::currentDateTime().date().toString("dd").toInt(&ok, 10);
	sethour		= QDateTime::currentDateTime().time().toString("hh").toInt(&ok, 10);
	setmin		= QDateTime::currentDateTime().time().toString("mm").toInt(&ok, 10);	
	setsecond	= QDateTime::currentDateTime().time().toString("ss").toInt(&ok, 10);

    pYearLab2->display(setyear);
    pMonthlab2->display(setmonth);
    pDaylab2->display(setday);
    pHouLab2->display(sethour);
    pMinLab2->display(setmin);
    pSencondLab2->display(setsecond);

    QPushButton *pYearButIn = new QPushButton(tr("+"));
    QPushButton *pYearButDe = new QPushButton(tr("-"));
    QPushButton *pMonthButIn = new QPushButton(tr("+"));
    QPushButton *pMonthButDe = new QPushButton(tr("-"));
    QPushButton *pDayButIn = new QPushButton(tr("+"));
    QPushButton *pDayButDe = new QPushButton(tr("-"));
    QPushButton *pHouButIn = new QPushButton(tr("+"));
    QPushButton *pHouButDe = new QPushButton(tr("-"));
    QPushButton *pMinButIn = new QPushButton(tr("+"));
    QPushButton *pMinButDe = new QPushButton(tr("-"));
    QPushButton *pSecButIn = new QPushButton(tr("+"));
    QPushButton *pSecButDe = new QPushButton(tr("-"));
    QPushButton *pCloseBut = new QPushButton(tr("关闭"));
    QPushButton *pSureBut = new QPushButton(tr("确认"));

    if(g_lcdsize == LCDSIZE_480x272)
    {
		bt_w = 30;
		bt_h = 25;

    }else {
		bt_w = 60;
		bt_h = 50;

    }

    pYearButIn->setMinimumSize(bt_w,bt_h);
    pYearButDe->setMinimumSize(bt_w,bt_h);
    pMonthButIn->setMinimumSize(bt_w,bt_h);
    pMonthButDe->setMinimumSize(bt_w,bt_h);
    pDayButIn->setMinimumSize(bt_w,bt_h);
    pDayButDe->setMinimumSize(bt_w,bt_h);
    pHouButIn->setMinimumSize(bt_w,bt_h);
    pHouButDe->setMinimumSize(bt_w,bt_h);
    pMinButIn->setMinimumSize(bt_w,bt_h);
    pMinButDe->setMinimumSize(bt_w,bt_h);
    pSecButIn->setMinimumSize(bt_w,bt_h);
    pSecButDe->setMinimumSize(bt_w,bt_h);
    pCloseBut->setMinimumSize(bt_w,bt_h);
    pSureBut->setMinimumSize(bt_w,bt_h);


    connect(pYearButIn,SIGNAL(clicked()),this,SLOT(setYearIn()));
    connect(pYearButDe,SIGNAL(clicked()),this,SLOT(setYearDe()));
    connect(pMonthButIn,SIGNAL(clicked()),this,SLOT(setMonthIn()));
    connect(pMonthButDe,SIGNAL(clicked()),this,SLOT(setMonthDe()));
    connect(pDayButIn,SIGNAL(clicked()),this,SLOT(setDayIn()));
    connect(pDayButDe,SIGNAL(clicked()),this,SLOT(setDayDe()));

    connect(pHouButIn,SIGNAL(clicked()),this,SLOT(setHouIn()));
    connect(pHouButDe,SIGNAL(clicked()),this,SLOT(setHouDe()));
    connect(pMinButIn,SIGNAL(clicked()),this,SLOT(setMinIn()));
    connect(pMinButDe,SIGNAL(clicked()),this,SLOT(setMinDe()));
    connect(pSecButIn,SIGNAL(clicked()),this,SLOT(setSecondIn()));
    connect(pSecButDe,SIGNAL(clicked()),this,SLOT(setSecondDe()));

    connect(pSureBut,SIGNAL(clicked()),this,SLOT(setTime()));
    connect(pCloseBut,SIGNAL(clicked()),this,SLOT(close()));


    QGridLayout *tmplayout = new QGridLayout;
    tmplayout->addWidget(pYearLab1, 0,0,1,1);
    tmplayout->addWidget(pYearButDe,0,1,1,1);
    tmplayout->addWidget(pYearLab2, 0,2,1,1);  
	tmplayout->addWidget(pYearButIn,0,3,1,1);

    tmplayout->addWidget(pMoomlab1,  1,0,1,1);
    tmplayout->addWidget(pMonthButDe,1,1,1,1);
    tmplayout->addWidget(pMonthlab2, 1,2,1,1);   
	tmplayout->addWidget(pMonthButIn,1,3,1,1);

    tmplayout->addWidget(pDaylab1,   2,0,1,1);
    tmplayout->addWidget(pDayButDe,  2,1,1,1);
    tmplayout->addWidget(pDaylab2,   2,2,1,1);   
	tmplayout->addWidget(pDayButIn,  2,3,1,1);

    tmplayout->addWidget(pHouLab1,   3,0,1,1);
    tmplayout->addWidget(pHouButDe,  3,1,1,1);
    tmplayout->addWidget(pHouLab2,   3,2,1,1);    
	tmplayout->addWidget(pHouButIn,  3,3,1,1);

    tmplayout->addWidget(pMinLab1,   4,0,1,1);
    tmplayout->addWidget(pMinButDe,  4,1,1,1);
    tmplayout->addWidget(pMinLab2,   4,2,1,1);   
	tmplayout->addWidget(pMinButIn,  4,3,1,1);

    tmplayout->addWidget(pSencondLab1,   5,0,1,1);
	tmplayout->addWidget(pSecButDe,      5,1,1,1); 
    tmplayout->addWidget(pSencondLab2,   5,2,1,1);
	tmplayout->addWidget(pSecButIn,      5,3,1,1);
    
    tmplayout->addWidget(pSureBut,       6,0,1,2);
    tmplayout->addWidget(pCloseBut,      6,2,1,2);

    if (DebugOrRelease)
    this->setCursor(Qt::CrossCursor);
    this->setLayout(tmplayout);
    this->setWindowFlags(Qt::FramelessWindowHint);
}

TimeSettings2::~TimeSettings2()
{

}

void TimeSettings2::setYearIn()
{
	qDebug()<<"setYearIn()";
   if(setyear < 2099)
     setyear++;
   pYearLab2->display(setyear);
}
void TimeSettings2::setYearDe()
{
   if(setyear>1900)
     setyear--;
   pYearLab2->display(setyear);
}
void TimeSettings2::setMonthIn()
{
   if(setmonth <= 12){
     setmonth++;
   }

   if(setmonth == 2)
   	if(setday > 28)
		setday = 28;
   
    if(setmonth > 12){
        setmonth = 1;
    }

	pMonthlab2->display(setmonth);
	pDaylab2->display(setday);
}
void TimeSettings2::setMonthDe()
{
   if(setmonth > 0)
     setmonth--;
   

   if(setmonth == 2)
   	if(setday > 28)
		setday = 28;

	if(setmonth <= 0)
		setmonth = 12;
   
   pMonthlab2->display(setmonth);
   pDaylab2->display(setday);
}
void TimeSettings2::setDayIn()
{
	int upper; 	//根据月份,日有不同的上限,大月31,小月30,二月分平年闰年

	if(setmonth==1 || setmonth==3 || setmonth==5 || setmonth==7 || setmonth==8 || setmonth==10 || setmonth==12){
		upper = 31;
	}

	if(setmonth==4 || setmonth==6 || setmonth==9 || setmonth==11){
		upper = 30;
	}

	if(setmonth == 2){
		if( ( setyear%4==0 && setyear%100!=0 )  ||  (setyear%400==0) ) //闰年
			upper = 29;
		else
			upper = 28;
	}

	if(setday <= upper ){
      	setday++;
	}
	if(setday > upper){
        setday = 1;
    }

	pDaylab2->display(setday);

}
void TimeSettings2::setDayDe()
{
    int upper; 	//根据月份,日有不同的上限,大月31,小月30,二月分平年闰年

	if(setmonth==1 || setmonth==3 || setmonth==5 || setmonth==7 || setmonth==8 || setmonth==10 || setmonth==12){
		upper = 31;
	}

	if(setmonth==4 || setmonth==6 || setmonth==9 || setmonth==11){
		upper = 30;
	}

	if(setmonth == 2){

		if( ( setyear%4==0 && setyear%100!=0 )  ||  (setyear%400==0) ) //闰年
			upper = 29;
		else
			upper = 28;
	}

	if(setday > 0 ){
      	setday--;
	}
	if((setday == 0) || (setday < 0)){
        setday = upper;
    }

	pDaylab2->display(setday);
}
void TimeSettings2::setHouIn()
{
    if(sethour <= 23 ){
      sethour++;
    }
    
     if((sethour > 23) || (sethour < 0)){
         sethour = 0;
     }

     pHouLab2->display(sethour);
}
void TimeSettings2::setHouDe()
{
    if(sethour >= 0)
      sethour--;
      
    if(sethour < 0)
        sethour=23;
    pHouLab2->display(sethour);
}
void TimeSettings2::setMinIn()
{
	if(setmin <= 59 ){
		setmin++;
	}

	if(setmin > 59){
		setmin = 0;
	}
	
	pMinLab2->display(setmin);
}
void TimeSettings2::setMinDe()
{
    if(setmin >= 0)
      setmin--;
      
    if(setmin < 0)
        setmin=59;
        
    pMinLab2->display(setmin);
}
void TimeSettings2::setSecondIn()
{
    if(setsecond <= 59 ){
      setsecond++;
    }
   
     if(setsecond > 59){
         setsecond = 0;
     }
     pSencondLab2->display(setsecond);
}
void TimeSettings2::setSecondDe()
{
    if(setsecond >= 0)
      setsecond--;
      
    if(setsecond < 0)
        setsecond=59;
    pSencondLab2->display(setsecond);
}
void TimeSettings2::setTime()
{
    struct tm timeSys;
    timeSys.tm_sec = setsecond;
    timeSys.tm_min =setmin;
    timeSys.tm_hour = sethour;
    timeSys.tm_mday = setday;
    timeSys.tm_mon = setmonth - 1;
    timeSys.tm_year = setyear - 1900;

    time_t timp;
    timp = mktime(&timeSys);

    struct timeval timeTv;
    timeTv.tv_sec = timp;
    timeTv.tv_usec=0;
  //  refreshWidgetFlag=0;
    settimeofday(&timeTv,NULL);
    system("hwclock -w");// zhi qian duan dian hou shi jian hai shi bu zhui de jie jue fang fa
    
	QDialog * setdialog = new QDialog;
	QLabel * settmp = new QLabel;

	settmp->setText(tr("设置时间成功"));
	settmp->setStyleSheet("color:green;");

	QTimer*timer = new QTimer(setdialog);
	timer->start(3000);
	connect(timer,SIGNAL(timeout()),setdialog,SLOT(close()));

	QGridLayout *tmplayout = new QGridLayout;

	tmplayout->addWidget(settmp,0,0,2,2);
	setdialog->setLayout(tmplayout);
	setdialog->setWindowFlags(Qt::FramelessWindowHint);
	setdialog->exec();
}

TimeSettings::TimeSettings(QWidget *parent) : JDialog(parent)
{
    refreshWidgetFlag =0;
    m_pYearSpinBox = new QLabel();
    m_pYearSpinBox->setText(QDateTime::currentDateTime().date().toString("yyyy"));

    m_pMoonSpinbox = new QLabel();
    m_pMoonSpinbox->setText(QDateTime::currentDateTime().date().toString("MM"));

    m_pDaySpinbox = new QLabel();
    m_pDaySpinbox->setText(QDateTime::currentDateTime().date().toString("dd"));

    m_pHourSpinBox = new QLabel();
    m_pHourSpinBox->setText(QDateTime::currentDateTime().time().toString("hh"));

    m_pMinSpinBox = new QLabel();
    m_pMinSpinBox->setText(QDateTime::currentDateTime().time().toString("mm"));

    m_pSencondSpinBox = new QLabel();
    m_pSencondSpinBox->setText(QDateTime::currentDateTime().time().toString("ss"));

    pHourLab = new QLabel(tr("时"));
    QLabel *pMinLab = new QLabel(tr("分"));
    QLabel *pSencondLab = new QLabel(tr("秒"));
    QLabel *pYearLab = new QLabel(tr("年"));
    QLabel *pMoomlab = new QLabel(tr("月"));
    QLabel *pDaylab = new QLabel(tr("日"));

/*
	pMinLab->setStyleSheet("border: 1px solid  #ff0000");
	m_pMinSpinBox->setStyleSheet("border: 1px solid  #ff0000");
*/
	pHourLab->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
	pMinLab->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
	pSencondLab->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
	pYearLab->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
	pMoomlab->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
	pDaylab->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);

	m_pYearSpinBox->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	m_pMoonSpinbox->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	m_pDaySpinbox->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	m_pHourSpinBox->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	m_pMinSpinBox->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
	m_pSencondSpinBox->setAlignment(Qt::AlignRight | Qt::AlignVCenter);

    QPushButton *pOKBtn = new QPushButton(tr("确认"));
    QPushButton *pSetBtn = new QPushButton(tr("设置"));
    pSetBtn->setMinimumSize(200,60);

    AnalogClock *pClockWindows = new AnalogClock();
    QFrame *pFrame = new QFrame();
    pFrame->setObjectName("timeSettingsFrame");

    QGridLayout *pSubLayout = new QGridLayout();

/*
    pSubLayout->addWidget(pClockWindows,       0, 0, 10, 10);
    pSubLayout->addWidget(m_pHourSpinBox,      1, 10, 1, 3);
    pSubLayout->addWidget(m_pMinSpinBox,       2, 10, 1, 3);
	pSubLayout->addWidget(m_pMinSpinBox,	   2, 12, 1, 1);

    pSubLayout->addWidget(m_pSencondSpinBox,   3, 10, 1, 3);
    pSubLayout->addWidget(m_pYearSpinBox,      5, 10, 1, 3);
    pSubLayout->addWidget(m_pMoonSpinbox,      6, 10, 1, 3);
    pSubLayout->addWidget(m_pDaySpinbox,       7, 10, 1, 3);
    pSubLayout->addWidget(pHourLab,            1, 13, 1, 2);
    pSubLayout->addWidget(pMinLab,             2, 13, 1, 2);
    pSubLayout->addWidget(pSencondLab,         3, 13, 1, 2);
    pSubLayout->addWidget(pYearLab,            5, 13, 1, 2);
    pSubLayout->addWidget(pMoomlab,            6, 13, 1, 2);
    pSubLayout->addWidget(pDaylab,             7, 13, 1, 2);
    pSubLayout->addWidget(pSetBtn,             8, 10, 1, 4);
   // pSubLayout->addWidget(pOKBtn,             9, 10, 1, 4);
*/
	pSubLayout->addWidget(pClockWindows,       0, 0, 10, 10);
    pSubLayout->addWidget(m_pYearSpinBox,      1, 11, 1, 1);
	pSubLayout->addWidget(m_pMoonSpinbox,	   2, 11, 1, 1);
    pSubLayout->addWidget(m_pDaySpinbox,       3, 11, 1, 1);
    pSubLayout->addWidget(m_pHourSpinBox,      5, 11, 1, 1);
    pSubLayout->addWidget(m_pMinSpinBox,       6, 11, 1, 1);
    pSubLayout->addWidget(m_pSencondSpinBox,   7, 11, 1, 1);
	
    pSubLayout->addWidget(pYearLab,            1, 12, 1, 1);
    pSubLayout->addWidget(pMoomlab,            2, 12, 1, 2);
    pSubLayout->addWidget(pDaylab,             3, 12, 1, 2);
    pSubLayout->addWidget(pHourLab,            5, 12, 1, 2);
    pSubLayout->addWidget(pMinLab,             6, 12, 1, 2);
    pSubLayout->addWidget(pSencondLab,         7, 12, 1, 2);
    pSubLayout->addWidget(pSetBtn,             8, 11, 1, 2);
	
    pSubLayout->setSpacing(10);

    pFrame->setLayout(pSubLayout);

    QGridLayout *pMainLayout = new QGridLayout();
    pMainLayout->addWidget(pFrame, 0, 0, 1, 1);

    connect(pOKBtn,  SIGNAL(clicked(bool)),  this,  SLOT(setDate()));
    connect(pSetBtn, SIGNAL(clicked()), this, SLOT(editDate()));

    QTimer *pTimer = new QTimer(this);
    pTimer->start(1000);

    connect(pTimer, SIGNAL(timeout()), this, SLOT(refreshWidget()));
    this->setJLayout(pMainLayout);
    this->setJTitle(tr("时钟测试"));

}

TimeSettings::~TimeSettings()
{
;
}

void TimeSettings::editDate()
{
//    m_pYearSpinBox->setEnabled(true);
//    m_pMoonSpinbox->setEnabled(true);
//    m_pDaySpinbox->setEnabled(true);
//    m_pHourSpinBox->setEnabled(true);
//    m_pMinSpinBox->setEnabled(true);
//    m_pSencondSpinBox->setEnabled(true);
//    refreshWidgetFlag=1;

//    QDialog * setdialog = new QDialog;

//    QLabel *pHouLab1 = new QLabel(tr("时"));
//    QLabel *pMinLab1 = new QLabel(tr("分"));
//    QLabel *pSencondLab1 = new QLabel(tr("秒"));
//    QLabel *pYearLab1 = new QLabel(tr("年"));
//    QLabel *pMoomlab1 = new QLabel(tr("月"));
//    QLabel *pDaylab1 = new QLabel(tr("日"));

//    QLabel *pHouLab2 = new QLabel(tr(" "));
//    QLabel *pMinLab2 = new QLabel(tr(" "));
//    QLabel *pSencondLab2 = new QLabel(tr(" "));
//   // QLabel *pYearLab2 = new QLabel(tr(" "));
//    QLabel *pMoomlab2 = new QLabel(tr(" "));
//    QLabel *pDaylab2 = new QLabel(tr(" "));
//    QLCDNumber *pYearLab2 = new QLCDNumber();

//    QPushButton *pYearButIn = new QPushButton(tr("+"));
//    QPushButton *pYeraButDe = new QPushButton(tr("-"));
//    QPushButton *pHouButIn = new QPushButton(tr("+"));
//    QPushButton *pHouButDe = new QPushButton(tr("-"));
//    QPushButton *pCloseBut = new QPushButton(tr("关闭"));
//    QPushButton *pSureBut = new QPushButton(tr("确认"));


//    connect(pYearLab2,SIGNAL(clicked()),this,SLOT(setYearIn()));

//    QGridLayout *tmplayout = new QGridLayout;
//    tmplayout->addWidget(pYearLab1, 0,0,1,1);
//    tmplayout->addWidget(pYearButIn,0,1,1,1);
//    tmplayout->addWidget(pYearLab2, 0,2,1,1);
//    tmplayout->addWidget(pYeraButDe,0,3,1,1);

////    tmplayout->addWidget(pYeraButDe,0,3,2,1);
////    tmplayout->addWidget(pYearLab1,0,0,1,1);
////    tmplayout->addWidget(pMoomlab1,0,0,1,1);
////    tmplayout->addWidget(pDaylab1,0,0,1,1);
////    tmplayout->addWidget(pHouLab1,0,0,1,1);
////    tmplayout->addWidget(pMinLab1,0,0,1,1);
////    tmplayout->addWidget(pSencondLab1,0,0,1,1);

//    setdialog->setLayout(tmplayout);
//  //  setdialog->setWindowFlags(Qt::FramelessWindowHint);
//    setdialog->exec();
    TimeSettings2 tmp;
    tmp.exec();

}

void TimeSettings::setDate()
{
    struct tm timeSys;
//    timeSys.tm_sec = m_pSencondSpinBox->value();
//    timeSys.tm_min = m_pMinSpinBox->value();
//    timeSys.tm_hour = m_pHourSpinBox->value();
//    timeSys.tm_mday = m_pDaySpinbox->value();
//    timeSys.tm_mon = m_pMoonSpinbox->value()-1;
  //  timeSys.tm_year = m_pYearSpinBox->value()-1900;

    m_pYearSpinBox->setEnabled(false);
    m_pMoonSpinbox->setEnabled(false);
    m_pDaySpinbox->setEnabled(false);
    m_pHourSpinBox->setEnabled(false);
    m_pMinSpinBox->setEnabled(false);
    m_pSencondSpinBox->setEnabled(false);

    time_t timp;
    timp =mktime(&timeSys);

    struct timeval timeTv;
    timeTv.tv_sec = timp;
    timeTv.tv_usec=0;
    refreshWidgetFlag=0;
	
    settimeofday(&timeTv,NULL);

	
    system("hwclock -w");// zhi qian duan dian hou shi jian hai shi bu zhui de jie jue fang fa

}

void TimeSettings::refreshWidget()
{
 if(1){
     m_pYearSpinBox->setText(QDateTime::currentDateTime().date().toString("yyyy"));
     m_pMoonSpinbox->setText(QDateTime::currentDateTime().date().toString("MM"));
     m_pDaySpinbox->setText(QDateTime::currentDateTime().date().toString("dd"));
     m_pHourSpinBox->setText(QDateTime::currentDateTime().time().toString("hh"));
     m_pMinSpinBox->setText(QDateTime::currentDateTime().time().toString("mm"));
     m_pSencondSpinBox->setText(QDateTime::currentDateTime().time().toString("ss"));

 }else{
     ;
 }

}

